//
//  PatientsViewController.swift
//  SidebarMenu
//
//  Created by LiangXiaobin on 2017/2/4.
//  Copyright © 2017年 AppCoda. All rights reserved.
//

import UIKit

class PatientsViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {

    @IBOutlet weak var menuBtn: UIBarButtonItem!
    var colletionView:UICollectionView?
    var editor:Bool?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        
        if revealViewController() != nil {
            menuBtn.target = revealViewController()
            menuBtn.action = #selector(SWRevealViewController.revealToggle(_:))
            view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        }
        
        let array = ["所有关注","特别关注","未写结论","今日告警"]
        let segment = ZFJSegmentedControl.init().initwithTitleArr(array, iconArr: nil, scType: .SCType_None)
        segment?.frame = CGRect(x:0,y:64 + 5,width:screenWidth,height:40)
        segment?.backgroundColor = baseColor
        segment?.titleColor = UIColor.gray
        segment?.selectTitleColor = UIColor.white
//        segment?.titleBtnBackColor = baseColor
//        segment?.borderWidth = 1.0
//        segment?.edgeInsetsSpace = CGFloat(ZFJButtonEdgeInsetsStyle.left.rawValue)
        segment?.selectType = {(selectIndex:NSInteger,selectIndexTit:String?) -> Void in
        }

        self.view.addSubview(segment!)
        
        
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = UICollectionViewScrollDirection.vertical  //滚动方向
        layout.itemSize = CGSize(width:315, height:150)
        
        colletionView = UICollectionView(frame:CGRect(x:0,y:64 + 40,width:screenWidth,height:screenHeight - 64 - 40), collectionViewLayout:layout)
        colletionView?.backgroundColor = UIColor.white
        colletionView?.delegate = self
        colletionView?.dataSource = self
        colletionView?.register(UINib(nibName: "PatientsCollectionViewCell", bundle: Bundle.main), forCellWithReuseIdentifier: "PatientsCollectionViewCell")
        
        colletionView?.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header")
        
        
        self.view.addSubview(colletionView!)

    }
    
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    
    // CollectionView行数
    func collectionView(_ collectionView: UICollectionView,
                        numberOfItemsInSection section: Int) -> Int {
        return 6;
    }
    
    // 获取单元格
    func collectionView(_ collectionView: UICollectionView,
                        cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        // storyboard里设计的单元格
        let identify:String = "PatientsCollectionViewCell"
        // 获取设计的单元格，不需要再动态添加界面元素
        let cell = self.colletionView?.dequeueReusableCell(
            withReuseIdentifier: identify, for: indexPath) as! PatientsCollectionViewCell
        cell.layer.borderWidth = 1
        cell.layer.borderColor = grayColor.cgColor
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        if kind == UICollectionElementKindSectionHeader {
            
            let header = colletionView?.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "header", for: indexPath)
            if editor == true
            {
                
            }
            else
            {
            }
            
            let psbtn = PsButton.mark()
            psbtn?.frame = CGRect(x:screenWidth - 80,y:0,width:80,height:40)
            header?.addSubview(psbtn!)
            psbtn?.setImage(UIImage(named:"author"), for: .normal)
            psbtn?.setTitle("编辑", for: UIControlState.normal)
            psbtn?.addTarget(self, action: #selector(PatientsViewController.editorBtnClicked(sender:)), for: .touchUpInside)
            return header!
            
        }
        else
        {
            let header = colletionView?.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "header", for: indexPath)
            
            return header!
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize
    {
        return CGSize(width:screenWidth,height:40)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
    {
        return UIEdgeInsets(top: 5,left: 0,bottom: 0,right: 0)
    }
    
    
    func editorBtnClicked(sender:UIButton)
    {
        
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
